<!DOCTYPE html>
<html>

<head>
    <title>资源分类</title>
    {include file="common/header"/}
    <el-form :inline="true">
        <el-form-item>
            <el-button icon="el-icon-plus" size="small" @click="clickAdd" plain>添加</el-button>
        </el-form-item>
        <el-form-item>
            <el-button icon="el-icon-warning-outline" size="small" @click="dayShow=true" plain>如何每日更新？</el-button>
        </el-form-item>
        <el-form-item>
            <el-button icon="el-icon-warning-outline" size="small" @click="allShow=true" plain>如何全部转存？</el-button>
        </el-form-item>
    </el-form>
    <el-table :data="dataList" @selection-change="changeSelection" v-loading="loading">
        <el-table-column type="selection" width="50">
        </el-table-column>
        <el-table-column prop="source_category_id" label="ID" width="60">
        </el-table-column>
        <el-table-column label="分类名称">
            <template slot-scope="scope">
                <img :src="scope.row.image" style="width: 30px;height:30px;vertical-align: middle;" v-if="scope.row.image" />
                <span style="vertical-align: middle;margin-left: 5px">{{scope.row.name}}</span>
            </template>
        </el-table-column>
        <el-table-column label="是否每日更新" width="150" align="center">
            <template slot-scope="scope">
                <el-switch v-model="scope.row.is_update==1?true:false"
                    @change="clickStatus(scope.row,1)">
                </el-switch>
            </template>
        </el-table-column>
        <el-table-column label="是否前台展示" width="150" align="center">
            <template slot-scope="scope">
                <el-switch v-model="scope.row.status==1?false:true"
                    @change="clickStatus(scope.row,0)">
                </el-switch>
            </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序"  width="150" align="center">
        </el-table-column>
        <el-table-column label="操作" width="200">
            <template slot-scope="scope">
                <el-link type="primary" @click="s2Btn(scope.row)" :underline="false">一键转存</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
                <el-link type="primary" @click="clickEdit(scope.row)" :underline="false">编辑</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
                <el-link type="danger" @click="clickDelete(scope.row)" :underline="false">删除</el-link>
            </template>
        </el-table-column>
    </el-table>


    <!-- 添加框 -->
    <el-dialog title="添加资源分类名称" :visible.sync="dialogFormAdd" width="500px" :modal-append-to-body='false' append-to-body :close-on-click-modal='false'>
        <el-form :model="formAdd" :rules="rules" ref="formAdd">
            
            <el-form-item prop="name" label="分类名称" :label-width="formLabelWidth">
                <el-input size="medium" autocomplete="off" v-model="formAdd.name"></el-input>
            </el-form-item>
            
            <el-form-item prop="image" label="图标" :label-width="formLabelWidth">
                <Single v-model="formAdd.image"></Single>
            </el-form-item>
            <el-form-item prop="sort" label="排序" :label-width="formLabelWidth">
                <el-input-number v-model="formAdd.sort" :min="0" :max="999" size="medium" style="width: 120px;"
                    controls-position="right" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="postAdd()">确认添加</el-button>
        </div>
    </el-dialog>
    <!-- 修改框 -->
    <el-dialog title="修改资源分类信息" :visible.sync="dialogFormEdit" width="500px" :modal-append-to-body='false' append-to-body :close-on-click-modal='false'>
        <el-form :model="formEdit" :rules="rules" ref="formEdit">
            <el-form-item prop="name" label="分类名称" :label-width="formLabelWidth">
                <el-input size="medium" autocomplete="off" v-model="formEdit.name" disabled ></el-input>
            </el-form-item>
            <el-form-item prop="image" label="图标" :label-width="formLabelWidth">
                <Single v-model="formEdit.image"></Single>
            </el-form-item>
            <el-form-item prop="sort" label="排序" :label-width="formLabelWidth">
                <el-input-number v-model="formEdit.sort" :min="0" :max="999" size="medium" style="width: 120px;"
                    controls-position="right" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="postEdit()">确认修改</el-button>
        </div>
    </el-dialog>
     <!-- 每日更新 -->
    <el-dialog title="每日更新" :visible.sync="dayShow" width="500px" :modal-append-to-body='false' append-to-body :close-on-click-modal='false'>
        <div style="margin-bottom: 20px;font-size:14px;color:#666;line-height: 2">
            <font color=orangered>功能暂停使用</font>
            <p>自动更新：<font color=orangered>转存当日及昨天的资源数据；</font></p>
            <font color=orangered>将此接口添加到计划任务中，计划任务每日执行一次即可；</font>
            <p>接口地址：{{domain}}/api/source/day</p>
            <p>Tips：添加计划任务后方可生效；名称重复的资源会跳过转存；</p>
            <p>宝塔任务类型：访问URL-GET</p>
        </div>
    </el-dialog>
    
     <!-- 全部更新 -->
    <el-dialog title="全部转存" :visible.sync="allShow" width="500px" :modal-append-to-body='false' append-to-body :close-on-click-modal='false'>
        <div style="margin-bottom: 20px;font-size:14px;color:#666;line-height: 2">
            <font color=orangered>功能暂停使用</font>
            <p>全部转存：<font color=orangered>一键转存所有资源到自己的网盘及系统中</font></p>
            <font color=orangered>全部转存速度比较慢，提交后请耐心等待；名称重复的资源会跳过转存；</font>
            <p>全部转存需要按类别转存：见下方列表一键转存按钮</p>
        </div>
    </el-dialog>

    {include file="common/footer"/}
    <script>
        var app = new Vue({
            el: '#app',
            data() {
                this.getList();
                return {
                    formLabelWidth: '80px',
                    dialogFormAdd: false,
                    dialogFormEdit: false,
                    loading: true,
                    dataList: [],
                    selectList: [],
                    formAdd: {
                        image: '',
                        sort: 0
                    },
                    formEdit: {
                    },
                    rules: {
                        name: [ { required: true, message: '请输入分类名称', trigger: 'blur' }],
                    },
                    
                    allShow: false,
                    dayShow: false,
                    domain: window.location.protocol+'//'+window.location.host
                }
            },
            methods: {
                changeSelection(list) {
                    var that = this;
                    that.selectList = [];
                    for (var index in list) {
                        that.selectList.push(list[index].source_category_id);
                    }
                },
                postEdit() {
                    var that = this;
                    that.$refs["formEdit"].validate((valid) => {
                        if (valid) {
                            axios.post('/admin/source_category/update', Object.assign({}, PostBase, that.formEdit))
                                .then(function (response) {
                                    that.getList();
                                    if (response.data.code == CODE_SUCCESS) {
                                        that.$message({
                                            message: response.data.message,
                                            type: 'success'
                                        });
                                        that.dialogFormEdit = false;
                                    } else {
                                        that.$message.error(response.data.message);
                                    }
                                })
                                .catch(function (error) {
                                    that.$message.error('服务器内部错误');
console.log(error);
                                });
                        }
                    });
                },
                postAdd() {
                    var that = this;
                    that.$refs['formAdd'].validate((valid) => {
                        if (valid) {
                            axios.post('/admin/source_category/add', Object.assign({}, PostBase, that.formAdd))
                                .then(function (response) {
                                    that.getList();
                                    if (response.data.code == CODE_SUCCESS) {
                                        that.$message({
                                            message: response.data.message,
                                            type: 'success'
                                        });
                                        that.dialogFormAdd = false;
                                    } else {
                                        that.$message.error(response.data.message);
                                    }
                                })
                                .catch(function (error) {
                                    that.$message.error('服务器内部错误');
console.log(error);
                                });
                        }
                    });
                },
                clickAdd() {
                    var that = this;
                    that.formAdd = { sort: 0,image: '' };
                    that.dialogFormAdd = true;
                },
                clickDelete(row) {
                    var that = this;
                    this.$confirm('即将删除这个分类, 是否确认?', '删除提醒', {
                        confirmButtonText: '删除',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post('/admin/source_category/delete', Object.assign({}, PostBase, {
                            source_category_id: row.source_category_id
                        }))
                            .then(function (response) {
                                that.getList();
                                if (response.data.code == CODE_SUCCESS) {
                                    that.$message({
                                        message: response.data.message,
                                        type: 'success'
                                    });
                                } else {
                                    that.$message.error(response.data.message);
                                }
                            })
                            .catch(function (error) {
                                that.$message.error('服务器内部错误');
console.log(error);
                            });
                    }).catch(() => {
                    });
                },
                clickStatus(row,type) {
                    var that = this;
                    let url = row.status ? '/admin/source_category/enable' : '/admin/source_category/disable'
                    if(type==1){
                        url = !row.is_update ? '/admin/source_category/enable' : '/admin/source_category/disable'
                    }
                    axios.post(url, Object.assign({}, PostBase, {
                        source_category_id: row.source_category_id,
                        type: type
                    }))
                        .then(function (response) {
                            that.getList();
                            if (response.data.code == CODE_SUCCESS) {
                                that.$message({
                                    message: response.data.message,
                                    type: 'success'
                                });
                            } else {
                                that.$message.error(response.data.message);
                            }
                        })
                        .catch(function (error) {
                            that.$message.error('服务器内部错误');
console.log(error);
                        });
                },
                clickEdit(row) {
                    var that = this;
                    that.formEdit = row;
                    axios.post('/admin/source_category/detail', Object.assign({}, PostBase, {
                        source_category_id: row.source_category_id
                    }))
                        .then(function (response) {
                            if (response.data.code == CODE_SUCCESS) {
                                that.formEdit = response.data.data;
                                that.formEdit.image = that.formEdit.image||''
                                that.dialogFormEdit = true;
                            } else {
                                that.$message.error(response.data.message);
                            }
                        })
                        .catch(function (error) {
                            that.$message.error('服务器内部错误');
console.log(error);
                        });

                },
                getList() {
                    var that = this;
                    that.loading = true;
                    axios.post('/admin/source_category/getList', Object.assign({}, PostBase))
                        .then(function (response) {
                            that.loading = false;
                            if (response.data.code == CODE_SUCCESS) {
                                that.dataList = response.data.data;
                            } else {
                                that.$message.error(response.data.message);
                            }
                        })
                        .catch(function (error) {
                            that.loading = false;
                            that.$message.error('服务器内部错误');
console.log(error);
                        });
                },
                

                s2Btn(row){
                    let that = this
                    if(row.source_category_id!=1) return that.$message.error('当前版本仅支持短剧');
                    this.$confirm('全部转存速度比较慢，提交后请耐心等待,该操作不可暂停, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post('/admin/source/transferAll', Object.assign({}, PostBase,{
                            source_category_id: row.source_category_id
                        }))
                            .then(function (res) {
                                if (res.data.code == 200) {
                                } else {
                                    that.$message.error(res.data.message);
                                }
                            })
                            .catch(function (error) {
                            });
                        that.$message({
                            message: "已提交任务，稍后查看结果",
                            type: 'success'
                        });
                    }).catch(() => {});
                    
                },
            }
        })
    </script>


</html>